package com.william.lifetraxer;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.WindowManager;
import android.widget.TextView;

import com.william.lifetraxer.utils.DbHandler;
import com.william.lifetraxer.utils.HttpHelper;
import com.william.lifetraxer.utils.User;

public class DuringRegister extends BasicActivity implements Runnable{
	
	private String usernameStr = null;
	private String passwordStr = null;
	private String birthdayStr = null;
	private String phoneStr = null;
	private String emailStr = null;
	private String genderStr = null;
	
	private boolean doRegisterInfo = true;
	private boolean registerResult = false;
	
	 
	private final int REGISTER_FAILED = -1;
	private final int CHANGE_INFO = 1;
	private final int TOAST_INFO = 2;
	private final int REGISTER_SUCCESS = 3;
	
	private int userid;
	
	private TextView textInfo = null;
	private DbHandler mUserDbHandler = new DbHandler(this);
	private Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			switch(msg.what){
			case CHANGE_INFO:
				DuringRegister.this.textInfo.setText(msg.obj.toString());
				break;
			case TOAST_INFO:
				DuringRegister.this.showMessageToast(msg.obj.toString(), 2500);
				break;
			}
			super.handleMessage(msg);
		}
		
	};

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){
			
		}	
		return false;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
				WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.during_register);
		textInfo = (TextView) findViewById(R.id.during_register_textview_info);
		getInfo();
		new Thread(this).start();
	}

	@Override
	public void run() {
		
		try{
			Thread.sleep(2000);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		registerUser();
		
		if(doRegisterInfo){
			
			try{
				Thread.sleep(2000);
			}catch(Exception e){
				e.printStackTrace();
			}
			
			registerInfo();
		}
	}
	
	private void getInfo(){
		Intent intent = getIntent();
		Bundle bundle = intent.getBundleExtra("fromRegister");
		
		usernameStr = bundle.getString("username");
		passwordStr = bundle.getString("password");
		birthdayStr = bundle.getString("birthday");
		phoneStr = bundle.getString("phone");
		emailStr = bundle.getString("email");
		genderStr = bundle.getString("gender");
		
		if("2".equals(genderStr)&&"".equals(birthdayStr)&&"".equals(phoneStr)&&"".equals(emailStr)){
			this.doRegisterInfo=false;
		}
	}
	
	private void registerUser(){
		
		Message msg1 = mHandler.obtainMessage();
		msg1.what = CHANGE_INFO;
		msg1.obj = this.getResources().getString(R.string.register_textview_register_essential_text);
		msg1.sendToTarget();
		msg1 = null;
		
		Message msg2 = mHandler.obtainMessage();
		msg2.what = TOAST_INFO;
		
		String useridStr = HttpHelper.registerUser(usernameStr, passwordStr);		
		userid = Integer.parseInt(useridStr);
		
		if(!"USER_EXIST".equals(useridStr)&&!"DATABASE_ERROR".equals(useridStr)){
			msg2.obj = this.getResources().getString(R.string.register_toast_register_essential_success_text);
			registerResult = true;
		}else if("USER_EXIST".equals(useridStr)){
			msg2.obj = this.getResources().getString(R.string.register_toast_user_exist_text);
			doRegisterInfo = false;
			registerResult = false;
		}else if("DATABASE_ERROR".equals(useridStr)){
			msg2.obj = this.getResources().getString(R.string.register_toast_register_failed_text);
			doRegisterInfo = false;
			registerResult = false;
		}
		msg2.sendToTarget();
		
		if(!doRegisterInfo){
			
			try{
				Thread.sleep(2000);
			}catch(Exception e){
				e.printStackTrace();
			}
			
			Message msg3 = mHandler.obtainMessage();
			msg3.what = this.CHANGE_INFO;
			int registerStatu = 0;
			if(registerResult){
				msg3.obj = this.getResources().getString(R.string.register_textview_register_success_text);
				registerStatu = REGISTER_SUCCESS;
			}else{
				msg3.obj = this.getResources().getString(R.string.register_toast_register_failed_text);
				registerStatu = REGISTER_FAILED;
			}
			msg3.sendToTarget();
			storeUserInfo();
			try{
				Thread.sleep(2000);
			}catch(Exception e){
				e.printStackTrace();
			}
			backToRegister(registerStatu);
		}
	}
	
	private void registerInfo(){
		Message msg1 = mHandler.obtainMessage();
		msg1.what = this.CHANGE_INFO;
		msg1.obj = this.getResources().getString(R.string.register_textview_register_unessential_text);
		msg1.sendToTarget();
		msg1 = null;
		
		Message msg2 = mHandler.obtainMessage();
		msg2.what = this.TOAST_INFO;
		
		String infoRigResult = HttpHelper.registerInfo(usernameStr, genderStr, birthdayStr, phoneStr, emailStr);
		
		if(!"USER_NOT_EXIST".equals(infoRigResult)&&!"DATABASE_ERROR".equals(infoRigResult)){
			msg2.obj = this.getResources().getString(R.string.register_toast_register_unessential_success_text);
			registerResult = true;
		}else if("USER_NOT_EXIST".equals(infoRigResult)){
			msg2.obj = this.getResources().getString(R.string.register_toast_user_not_exist_text);
			registerResult = false;
		}else if("DATABASE_ERROR".equals(infoRigResult)){
			msg2.obj = this.getResources().getString(R.string.register_toast_register_failed_text);
			registerResult = false;
		}
		msg2.sendToTarget();
		
		int registerStatu = this.REGISTER_FAILED;
		if(registerResult){
			
			try{
				Thread.sleep(2000);
			}catch(Exception e){
				e.printStackTrace();
			}
			
			Message msg3 = mHandler.obtainMessage();
			msg3.what = this.CHANGE_INFO;
			msg3.obj = this.getResources().getString(R.string.register_textview_register_success_text);
			msg3.sendToTarget();
			registerStatu = this.REGISTER_SUCCESS;
			storeUserInfo();
		}
		
		try{
			Thread.sleep(2000);
		}catch(Exception e){
			e.printStackTrace();
		}
		backToRegister(registerStatu);
	}
	
	private void storeUserInfo(){
		try{
			Thread.sleep(1000);
		}catch(Exception e){
			e.printStackTrace();
		}
		Message msg = mHandler.obtainMessage();
		msg.what = this.CHANGE_INFO;
		msg.obj = this.getResources().getString(R.string.register_toast_store_user_info_text);
		msg.sendToTarget();
		User user = new User(usernameStr,userid,1,"to be finished");
		
		this.mUserDbHandler.insertUser(user);
		
		Message msg1 = mHandler.obtainMessage();
		msg1.what = this.TOAST_INFO;
		msg1.obj = this.getResources().getString(R.string.register_toast_store_user_info_success_text);
		msg1.sendToTarget();
	}

	private void backToRegister(int registerResult){
		this.setResult(registerResult);
		this.finish();
	}

}
